﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MediaScraper.IO
{
    
    public class FileManager
    {
        public FileManager()
        {
            
        }
        
      
        
        public void MoveFile(string source, string destination,bool overwrite = true)
        {
            if (!FileExists(destination))
            {
                File.Move(source, destination);
            }
            else
            {
                if (overwrite)
                {
                    File.Delete(destination); 
                    File.Move(source, destination);
                }
            }
      
        }

        public void RenameFile(string fullPath, string updateFileName,bool overwrite = true)
        {
            if (FileExists(fullPath))
            {
                var path = Path.GetDirectoryName(fullPath);
                var ext = Path.GetExtension(fullPath);
                var destinationPath = string.Format("{0}\\{1}{2}", path, updateFileName, ext);

                if (!FileExists(destinationPath))
                {
                    File.Move(fullPath, destinationPath);    
                }
                else
                {
                    if (overwrite)
                    {
                        File.Delete(destinationPath);
                        File.Move(fullPath, destinationPath);
                    }
                }
            }
            else
            {
                //ToDo: Logging
            }
        }

        public void TryCreateFolder(string path)
        {
            if (!DirectoryExists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
            {
                //ToDo: Logging
            }
        }

        public void TryCreateSubFolder(string path,string subfolder)
        {
            var destination = path + subfolder;
            if (!DirectoryExists(destination))
            {
                TryCreateFolder(destination);
            }
        }

        private static bool FileExists(string filePath)
        {
            //UNC path
            if (filePath.StartsWith(@"\"))
            {
                return UNCFileExists(filePath);
            }
            return File.Exists(filePath);
        }
      
        private static bool UNCFileExists(string uncPath)
        {
            try
            {
                FileInfo fi = new FileInfo(uncPath);
                return fi.Exists;
            }
            catch { }
            return false;
        }

        public static bool DirectoryExists(string path)
        {
            //UNC path
            if (path.StartsWith(@"\"))
            {
                return UNCDirectoryExists(path);
            }
            return Directory.Exists(path);
        }

        private static bool UNCDirectoryExists(string uncPath)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(uncPath);
                return di.Exists;
            }
            catch { }
            return false;
        }
    }
}
